package com.it.gmall.all.controller;

import com.it.gmall.list.client.ListFeignClient;
import com.it.gmall.model.list.*;
import com.it.gmall.model.product.BaseTrademark;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.net.URLEncoder;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
public class ListController {
    @Autowired
    private ListFeignClient listFeignClient;

    // 去搜索
    @GetMapping("/list.html")
    public String searchGoods(SearchParam searchParam, Model model){
        System.out.println(searchParam);
        SearchResponseVo searchResponseVo = listFeignClient.searchItem(searchParam);
        // 0.页面地址构建
        String urlParam = this.buildUrlParam(searchParam);
        model.addAttribute("urlParam",urlParam);
        // 1.回显关键字
        model.addAttribute("searchParam",searchParam);
        // 2.回显品牌列表
        model.addAttribute("trademarkList",searchResponseVo.getTrademarkList());
        // 3.回显商品列表
        model.addAttribute("goodsList",searchResponseVo.getGoodsList());
        // 4.回显销售属性列表
        model.addAttribute("attrsList",searchResponseVo.getAttrsList());
        // 5.回显分页
        model.addAttribute("totalPages",searchResponseVo.getTotalPages()); // 总页数
        model.addAttribute("pageNo",searchResponseVo.getPageNo()); // 当前页
        // 6.回显三级分类条件
        // 6.1 回显品牌
        String trademark = searchParam.getTrademark();
        if(!StringUtils.isEmpty(trademark)){
            model.addAttribute("trademarkParam","品牌:"+trademark.split(":")[1]);
        }
        // 6.2 回显属性
        List<Map> propsParamList = this.buildPropsParamList(searchParam);
        model.addAttribute("propsParamList",propsParamList);
        // 7.回显排序
        Map orderMap = this.buildOrderMap(searchParam);
        model.addAttribute("orderMap",orderMap);

        return "list/index";
    }

    // 构建排序
    private Map buildOrderMap(SearchParam searchParam) {
        String order = searchParam.getOrder();
        Map map = new HashMap<>();
        if(!StringUtils.isEmpty(order)){ // 没有穿参数 走默认 hotscore desc
            String[] split = order.split(":");
            map.put("type",split[0]);
            map.put("sort",split[1]);
        }else {
            map.put("type","1");
            map.put("sort","desc");
        }
        return map;
    }

    // 构建属性
    private List<Map> buildPropsParamList(SearchParam searchParam) {
        String[] props = searchParam.getProps();
        if(null!=props&&props.length>0){
            List<Map> list =
                    Arrays.stream(props).map((p)->{
                        Map map = new HashMap();
                        String[] split = p.split(":");
                        map.put("attrId",split[0]);
                        map.put("attrValue",split[1]);
                        map.put("attrName",split[2]);
                        return map;
                    }).collect(Collectors.toList());
            return list;
        }
        return null;
    }

    // 构建地址栏的url参数
    private String buildUrlParam(SearchParam searchParam) {
        StringBuilder urlParam = new StringBuilder();
        // keyword
        urlParam.append("http://list.gmall.com/list.html?keyword="+searchParam.getKeyword());
        // 品牌
        String trademark = searchParam.getTrademark();
        if(!StringUtils.isEmpty(trademark)){
            urlParam.append("&trademark="+trademark);
        }
        // 平台属性
        String[] props = searchParam.getProps();
        if(null!=props && props.length>0){
            for (String prop : props) {
                urlParam.append("&props="+prop);
            }
        }

        return urlParam.toString();
    }
}
